package com.source.adnroid.comm.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.source.android.androidcomm.R;

import java.util.ArrayList;

/**
 * Created by zzw on 2018/4/2.
 */

public class ChatMessageAdapter extends RecyclerView.Adapter<ChatMessageAdapter.ViewHolder>{
    private ArrayList<String> mData;
    OnItemClickListener onItemClickListener;
    public void setOnItemClickListener(OnItemClickListener onItemClickListener ){
        this. onItemClickListener=onItemClickListener;
    }
    public ChatMessageAdapter(ArrayList<String> data) {
        this.mData = data;
    }
    //更新数据
    public void updateData(ArrayList<String> data) {
        this.mData = data;
        notifyDataSetChanged();
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_msg, parent, false);
        // 实例化viewholder
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;

    }
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        // 绑定数据
        holder.chatToMsg.setText(mData.get(position));
        holder.chatFromMsg.setText(mData.get(position));
        holder.fromImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onClick(position);
            }
        });
        holder.toImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onClick(position);
            }
        });
   /*     if (position%2==0){
            holder.chatToMsg.setText(mData.get(position));
            holder.chatFromContent.setVisibility(View.INVISIBLE);
        }else {
            holder.chatFromMsg.setText(mData.get(position));
            holder.chatToContent.setVisibility(View.INVISIBLE);
        }*/

    }

    @Override
    public int getItemCount() {
        return mData.size();
    }
    public  class ViewHolder extends RecyclerView.ViewHolder {
        TextView chatToMsg;
        TextView chatFromMsg;
        TextView chatFromName;
        TextView chatToName;
        LinearLayout chatToContent;
        LinearLayout chatFromContent;
        ImageView toImg;
        ImageView fromImg;
        public ViewHolder(View itemView) {
            super(itemView);
            chatToMsg =itemView.findViewById(R.id.chat_to_msg);
            chatFromMsg =itemView.findViewById(R.id.chat_from_msg);
            chatToContent=itemView.findViewById(R.id.chat_to_content);
            chatFromContent=itemView.findViewById(R.id.chat_from_content);
            chatFromName=itemView.findViewById(R.id.chat_from_name);
            chatToName=itemView.findViewById(R.id.chat_to_name);
            toImg=itemView.findViewById(R.id.to_img);
            fromImg=itemView.findViewById(R.id.from_img);
        }
    }
}
